import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Vector;

public class P2PCompFile extends P2PFile {
	
	public Vector<Integer> availableChunks = new Vector<Integer>();	
	
	P2PCompFile(String filename) throws Exception {
		super();
		File tmp = new File(filename);
		SHAChecksum hashOfFile = new SHAChecksum();
		sha_sb = hashOfFile.getStringSHAChecksum(filename);
		sha_md = hashOfFile.getMdSHAChecksum(filename);
		this.size = tmp.length();
		this.filename = tmp.getName();			
		
		for (int i=0; i <= ((int)size-1)/1024; i++) {
			availableChunks.add(i);
		}

	}
	
	public byte[] getChunk(int num) throws IOException {	//Retorna o chunk "num", se existir
		
		if ((num < availableChunks.size()) && (availableChunks.get(num) != null)) {
			byte[] buf = new byte[1024];
			InputStream tmp = new FileInputStream("share/" + this.filename);
			for (int i=0; i<=num;i++) 
				tmp.read(buf,0,1024);
			
			return buf;
		}
		else
			return null;
		
	}

	@Override
	public boolean hasChunk() {
		if (!availableChunks.isEmpty())
			return true;
		else
			return false;
	}
	
	public boolean hasChunk(int num) {
		if (availableChunks.contains(num))
			return true;
		else
			return false;
	}

}
